"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const weixin_request_1 = require("./weixin-request");
const common_1 = require("./common");
class MCH {
    constructor(configReader) {
        this.cr = configReader;
    }
    url(path) {
        if (this.cr.getIsSandBox()) {
            return `${MCH.sandboxBaseURL}/${path}`;
        }
        return `${MCH.baseURL}/${path}`;
    }
    async getsignkey() {
        let partnerId = this.cr.getParanerId();
        let partnerKey = this.cr.getParanerKey();
        let url = this.url("pay/getsignkey");
        let nonce_str = common_1.getNonceStr();
        let sign = common_1.getMD5Sign(partnerKey, { mch_id: partnerId, nonce_str });
        let args = { sign, mch_id: partnerId, nonce_str };
        let obj = await weixin_request_1.WeiXinRequest.postByXML(url, args);
        return obj;
    }
    async paySign(args) {
        let key = this.cr.getAppKey();
        let sign = common_1.getMD5Sign(key, args);
        return sign;
    }
    async unifiedorder(args) {
        let url = this.url("pay/unifiedorder");
        let nonce_str = common_1.getNonceStr();
        let spbill_create_ip = "127.0.0.1";
        let parameterValue2 = "JSAPI";
        args["appid"] = this.cr.getAppId();
        args["mch_id"] = this.cr.getParanerId();
        args["nonce_str"] = nonce_str;
        args["trade_type"] = parameterValue2;
        args["spbill_create_ip"] = spbill_create_ip;
        if (this.cr.getIsSandBox()) {
            args.total_fee = 101;
        }
        // attach 最长为 127
        console.assert((args.attach || '').length <= 127);
        let key = await this.getParanerKey();
        let sign = common_1.getMD5Sign(key, args);
        args["sign"] = sign;
        let obj = await weixin_request_1.WeiXinRequest.postByXML(url, args);
        return obj;
    }
    async micropay(args) {
        let url = this.url("pay/micropay");
        args["appid"] = this.cr.getAppId();
        args["mch_id"] = this.cr.getParanerId();
        args["nonce_str"] = common_1.getNonceStr();
        args["spbill_create_ip"] = "8.8.8.8";
        args["auth_code"] = "120061098828009406";
        let key = await this.getParanerKey();
        let sign = common_1.getMD5Sign(key, args);
        args["sign"] = sign;
        let obj = await weixin_request_1.WeiXinRequest.postByXML(url, args);
        return obj;
    }
    async orderquery(args) {
        let url = this.url("pay/orderquery");
        args["appid"] = this.cr.getAppId();
        args["mch_id"] = this.cr.getParanerId();
        args["nonce_str"] = common_1.getNonceStr();
        let key = await this.getParanerKey();
        let sign = common_1.getMD5Sign(key, args);
        args["sign"] = sign;
        let obj = await weixin_request_1.WeiXinRequest.postByXML(url, args);
        return obj;
    }
    async downloadbill(args) {
        args["appid"] = this.cr.getAppId();
        args["mch_id"] = this.cr.getParanerId();
        args["nonce_str"] = common_1.getNonceStr();
        args.bill_type = "ALL";
        let key = await this.getParanerKey();
        args["sign"] = common_1.getMD5Sign(key, args);
        let url = this.url("pay/downloadbill");
        let obj = await weixin_request_1.WeiXinRequest.postByXML(url, args);
        return obj;
    }
    async getParanerKey() {
        if (this.cr.getIsSandBox() == false)
            return this.cr.getParanerKey();
        if (!this.sandboxPartnerKey) {
            let r = await this.getsignkey();
            this.sandboxPartnerKey = await r.sandbox_signkey;
        }
        return this.sandboxPartnerKey;
    }
}
exports.MCH = MCH;
MCH.baseURL = "https://api.mch.weixin.qq.com";
MCH.sandboxBaseURL = "https://api.mch.weixin.qq.com/sandboxnew";
